<template>
  <el-dialog title="修改物资信息" :visible.sync="localVisible" @close="handleClose">
    <el-form ref="searchForm" :model="searchForm" label-width="120px">
      <el-form-item label="设备型号" prop="searchModel">
        <el-input v-model="searchForm.searchModel" @blur="handleSearch"></el-input>
      </el-form-item>
    </el-form>
    <el-table v-if="searchResults.length > 0" :data="searchResults" @row-click="handleSelectRow">
      <el-table-column prop="type" label="设备类型"></el-table-column>
      <el-table-column prop="model" label="设备型号"></el-table-column>
      <el-table-column prop="manufacturer" label="设备厂商"></el-table-column>
      <el-table-column prop="area" label="设备使用区域"></el-table-column>
    </el-table>
    <el-form ref="form" v-if="showEditForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="设备类型" prop="type">
        <el-input v-model="form.type"></el-input>
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input v-model="form.model" disabled></el-input>
      </el-form-item>
      <el-form-item label="设备厂商" prop="manufacturer">
        <el-input v-model="form.manufacturer"></el-input>
      </el-form-item>
      <el-form-item label="设备使用区域" prop="area">
        <el-select v-model="form.area" placeholder="请选择区域">
          <el-option v-for="area in areas" :key="area" :label="area" :value="area"></el-option>
        </el-select>
      </el-form-item>
      
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
      </div>
  </el-dialog>
</template>

<script>
import axios from 'axios';

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      localVisible: this.visible,
      searchForm: {
        searchModel: ''
      },
      searchResults: [],
      showEditForm: false,
      areas:[],
      form: {
        model: '',
        type: '',
        manufacturer: '',
        area: ''
      },
      rules: {
        type: [{ required: true, message: '请输入设备类型', trigger: 'blur' }],
        manufacturer: [{ required: true, message: '请输入设备厂商', trigger: 'blur' }],
        area: [{ required: true, message: '请选择设备使用区域', trigger: 'change' }]
      }
    };
  },
  watch: {
    visible(newVal) {
      this.localVisible = newVal;
    },
    localVisible(newVal) {
      this.$emit('update:visible', newVal);
    },

  },
  methods: {
    handleClose() {
      this.localVisible = false;
    },
    handleSearch() {
      console.log(this.searchForm.searchModel)
      axios.post(`/api/stock/search`, this.searchForm.searchModel, {
        headers: {
          'Content-Type': 'text/plain'
        }
      }).then((response) => {
        if (response.data.success) {
          this.searchResults = response.data.data;
        } else {
          this.$message.error('未发现该设备型号');
        }
      }).catch((error) => {
        this.$message.error('查询失败');
      });
    },
    handleSelectRow(row) {
      this.form = { ...row };
      this.showEditForm = true;
      this.searchResults = [];
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          axios.post('/api/stock/edit', this.form).then((response) => {
            if (response.data.success) {
              this.$message.success('修改成功');
              this.handleClose();
            } else {
              this.$message.error('修改失败');
            }
          }).catch((error) => {
            this.$message.error('修改失败');
          });
        }
      });
    }
  },
  mounted(){
        axios.post('/api/stock/areas')
        .then(res=>{
            this.areas=res.data;
        })
    }
};
</script>